package sparrowGui.components.item
{
	import flash.display.DisplayObjectContainer;
	import flash.events.MouseEvent;
	
	import sparrowGui.components.BaseUIComponent;
	import sparrowGui.event.ItemEvent;
	import sparrowGui.i.IItem;
	import sparrowGui.i.ITreeNode;
	
	/** 项折叠状态改变. **/
	[Event(name="item_fold_change", 	type="sparrowGui.event.ItemEvent")]
	[Event(name="item_select_change", 	type="sparrowGui.event.ItemEvent")]
	
	/**
	 * 树形控件的型
	 * 多了开合起的按钮事件
	 * 
	 * 例子如下
	 * 
	 * var itm:SItem = new STreeItem();
		itm.update("按钮文字");
		addChild(itm);
	 * 
	 * @author Pelephone
	 * @website http://cnblogs.com/pelephone
	 */
	public class STreeItem extends BaseUIComponent implements IItem
	{
		static public const SELECTSTATE_NAME:String = "selectState";
		static public const ENABLEDSTATE_NAME:String = "enabledState";
		static public const FOLDSTATE_NAME:String = "foldState";
		
		private static const FOLDBTN_NAME:String = "foldBtn";		//合起按钮
		private static const SELECTBTN_NAME:String = "selectBtn";		//选中区按钮
		
		/**
		 * 打开合起按钮
		 */
		private var foldItem:IItem;
		/**
		 * 可选按钮
		 */
		private var selectItem:IItem;
		
		private var _data:Object;
		
		/**
		 * 构造树形组件的项
		 * @param uiVars 皮肤变量
		 */
		public function STreeItem(uiVars:Object=null)
		{
			super(uiVars);
			addSkinListen();
		}
		
		// <TreeNodeVO>
		public function update(o:Object):void
		{
			_data = o;
			if(o is ITreeNode)
				selectItem.update(o.txtName);
			else
				selectItem.update(o);
			
//			foldItem.selected = !o.folded;
//			selectItem.selected = o.selected;
		}
		
		override protected function addSkinListen():void
		{
			if(foldItem)
				foldItem.skin.addEventListener(MouseEvent.CLICK,onFoldEvt);
			if(selectItem)
				selectItem.skin.addEventListener(MouseEvent.CLICK,onSelectEvt);
		}
		
		override protected function removeSkinListen():void
		{
			if(foldItem)
				foldItem.skin.removeEventListener(MouseEvent.CLICK,onFoldEvt);
			if(selectItem)
				selectItem.skin.removeEventListener(MouseEvent.CLICK,onSelectEvt);
		}
		
		protected function onFoldEvt(e:MouseEvent):void
		{
			dispatchEvent(new ItemEvent(ItemEvent.ITEM_FOLD_CHANGE));
		}
		
		protected function onSelectEvt(e:MouseEvent):void
		{
			dispatchEvent(new ItemEvent(ItemEvent.ITEM_SELECT_CHANGE));
		}
		
		override protected function setUI(uiVars:Object=null):void
		{
			super.setUI(uiVars);
			
			if(skin is DisplayObjectContainer){
				var skinDC:DisplayObjectContainer = skin as DisplayObjectContainer;
				foldItem = new SListItem(skinDC.getChildByName(FOLDBTN_NAME));
				selectItem = new SListItem(skinDC.getChildByName(SELECTBTN_NAME));
			}
			
/*			argSkin = argSkin || SkinStyleSheet.getIns().treeItemSkin;
			
			foldItem = new RichItem(argSkin[FOLDBTN_NAME]);
			selectItem = new RichItem(argSkin[SELECTBTN_NAME]);
			
			_skin = argSkin;
			name = _skin.name;*/

//			changeState(UPSTATE_NAME);
//			super.create(argSkin);
		}
		
/*		override protected function onMouseEvt(e:Event):void
		{
			switch(e.target)
			{
				case foldBtn:
				{
					dispatchEvent(new ItemEvent(ItemEvent.ITEM_FOLD_CHANGE));
					break;
				}
				case selectBtn:
				{
					dispatchEvent(new ItemEvent(ItemEvent.ITEM_SELECT_CHANGE));
					break;
				}
			}
		}*/
		
/*		public function reCreate(argSkin:Sprite):void
		{
			
		}*/
		
		///////////////////////////////////
		// get/set
		//////////////////////////////////
		
		
		override public function getDefaultUIName():String
		{
			return "treeItem";
		}
		
		public function set folded(value:Boolean):void
		{
			foldItem.setState(SListItem.SELECTSTATE_NAME,value);
			dispatchEvent(new ItemEvent(ItemEvent.ITEM_FOLD_CHANGE));
		}
		
/*		public function get folded():Boolean
		{
			return foldItem.selected;
		}*/
		
		public function set selected(value:Boolean):void
		{
			selectItem.setState(SListItem.SELECTSTATE_NAME,value);
			dispatchEvent(new ItemEvent(ItemEvent.ITEM_SELECT_CHANGE));
		}
		
/*		public function get selected():Boolean 
		{
			return selectItem.selected;
		}*/
		
		public function get itemData():Object
		{
			return _data;
		}
		
		override public function dispose():void
		{
			selectItem.dispose();
			foldItem.dispose();
		}
		
		public function setState(stateName:String, value:Object=null):void
		{
			switch(stateName)
			{
				case FOLDSTATE_NAME:
				{
					foldItem.setState(SListItem.SELECTSTATE_NAME,value);
					return;
				}
				case SELECTSTATE_NAME:
				{
					selectItem.setState(SListItem.SELECTSTATE_NAME,value);
					return;
				}
				case ENABLEDSTATE_NAME:
				{
					selectItem.setState(SListItem.ENABLEDSTATE_NAME,value);
					foldItem.setState(SListItem.ENABLEDSTATE_NAME,value);
					return;
				}
			}
		}
	}
}